﻿Imports System.IO
Imports System.Windows.Forms

Public Class PlayerCharacter

    Private _id As Integer = -1
    Private _userID As Integer = -1
    Private _name As String = ""
    Private _str As Integer = 0
    Private _dex As Integer = 0
    Private _int As Integer = 0
    Private _hp As Integer = 0
    Private _sp As Integer = 0
    Private _male As Boolean
    Private _raceID As Integer = -1
    Private _racePortraitName As String = ""
    Private _classID As Integer = -1
    Private _guildID As Integer = -1

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property UserID() As Integer
        Get
            Return _userID
        End Get
        Set(ByVal value As Integer)
            _userID = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Strength() As Integer
        Get
            Return _str
        End Get
        Set(ByVal value As Integer)
            _str = value
        End Set
    End Property

    Public Property Dexterity() As Integer
        Get
            Return _dex
        End Get
        Set(ByVal value As Integer)
            _dex = value
        End Set
    End Property

    Public Property Intelligence() As Integer
        Get
            Return _int
        End Get
        Set(ByVal value As Integer)
            _int = value
        End Set
    End Property

    Public Property HitPoints() As Integer
        Get
            Return _hp
        End Get
        Set(ByVal value As Integer)
            _hp = value
        End Set
    End Property

    Public Property SpellPoints() As Integer
        Get
            Return _sp
        End Get
        Set(ByVal value As Integer)
            _sp = value
        End Set
    End Property

    Public Property IsMale() As Boolean
        Get
            Return _male
        End Get
        Set(ByVal value As Boolean)
            _male = value
        End Set
    End Property

    Public Property RaceID() As Integer
        Get
            Return _raceID
        End Get
        Set(ByVal value As Integer)
            _raceID = value
        End Set
    End Property

    Public Property RacePortraitName() As String
        Get
            Return _racePortraitName
        End Get
        Set(ByVal value As String)
            _racePortraitName = value
        End Set
    End Property

    Public Property ClassID() As Integer
        Get
            Return _classID
        End Get
        Set(ByVal value As Integer)
            _classID = value
        End Set
    End Property

    Public Property GuildID() As Integer
        Get
            Return _guildID
        End Get
        Set(ByVal value As Integer)
            _guildID = value
        End Set
    End Property

    Public Function ToXElement() As XElement
        Dim aChar = <character id=<%= _id.ToString %> userID=<%= _userID.ToString %>>
                        <name><%= _name %></name>
                        <stats str=<%= _str.ToString %> dex=<%= _dex.ToString %> int=<%= _int.ToString %>/>
                        <status hp=<%= _hp.ToString %> sp=<%= _sp.ToString %>/>
                        <race id=<%= _raceID.ToString %> male=<%= _male.ToString %> image=<%= _racePortraitName %>/>
                        <guild id=<%= _guildID %> class=<%= _classID %>/>
                    </character>
        Return aChar
    End Function

    Public Sub LoadXML(ByVal characterXML As String)
        Dim sr As New StringReader(characterXML)
        Dim charX As XElement

        Try
            charX = XElement.Load(sr)
        Catch ex As Exception
            Failed(characterXML, ex.ToString)
            Exit Sub
        End Try

        If Integer.TryParse(charX.@id, _id) = False Then
            Failed(characterXML, "ID invalid integer")
            Exit Sub
        End If
        If Integer.TryParse(charX.@userID, _userID) = False Then
            Failed(characterXML, "UserID invalid integer")
            Exit Sub
        End If
        _name = charX.<name>.Value
        If Integer.TryParse(charX.<stats>.@str, _str) = False Then
            Failed(characterXML, "Str invalid integer")
            Exit Sub
        End If
        If Integer.TryParse(charX.<stats>.@dex, _dex) = False Then
            Failed(characterXML, "Dex invalid integer")
            Exit Sub
        End If
        If Integer.TryParse(charX.<stats>.@int, _int) = False Then
            Failed(characterXML, "Int invalid integer")
            Exit Sub
        End If
        If Integer.TryParse(charX.<status>.@hp, _hp) = False Then
            Failed(characterXML, "Hp invalid integer")
            Exit Sub
        End If
        If Integer.TryParse(charX.<status>.@sp, _sp) = False Then
            Failed(characterXML, "Sp invalid integer")
            Exit Sub
        End If
        If Integer.TryParse(charX.<race>.@id, _raceID) = False Then
            Failed(characterXML, "RaceID invalid integer")
            Exit Sub
        End If
        If Boolean.TryParse(charX.<race>.@male, _male) = False Then
            Failed(characterXML, "Male invalid boolean")
            Exit Sub
        End If
        _racePortraitName = charX.<race>.@image
        If Integer.TryParse(charX.<guild>.@id, _guildID) = False Then
            Failed(characterXML, "GuildID invalid integer")
            Exit Sub
        End If
        If Integer.TryParse(charX.<guild>.@class, _classID) = False Then
            Failed(characterXML, "ClassID invalid integer")
            Exit Sub
        End If
    End Sub

    Private Sub Failed(ByVal xmlString As String, ByVal ex As String)
        MessageBox.Show(xmlString + vbCrLf + vbCrLf + ex, "Failed to LoadXML", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub
End Class
